#!/bin/bash

#
# Common function libraries of installation shells.
# 
# Target platform: Based on Ubuntu(Ubuntu Kylin/Ubuntu Desktop/Linux Mint/ZorinOS/DeepinOS), include graphical interface, and not applicable to any server version.
# Create date: 2019/06/26
# Create user: SteveJrong
# File version: 1.0.0

# ######################### start of constants #########################
USER_PATH=`echo ${HOME}`
TEMP_FILE_PATH=`echo "$USER_PATH"/temporary_directory`

# App packages installation mdoe enumerations.
APP_PACKAGE_INSTALLATION_MODE_DPKG_APT="dpkg"
APP_PACKAGE_INSTALLATION_MODE_SNAP="snap"
APP_PACKAGE_INSTALLATION_MODE_RPM="rpm"
APP_PACKAGE_INSTALLATION_MODE_YUM="yum"
# ######################### end of constants #########################


# ######################### start of functions #########################
#
# Query the full name based on the keyword of the app package.
# Infact, <reference passing> of shell scripts is used here.
# On Using 'eval' function to solve the problem of return value of function, see also: https://blog.csdn.net/u014268348/article/details/81567349
#
# Input paramters: 
#	$1 - no default value, this is app package name to fuzzy query.
#	$2 - no default value, this is enumerated values to how the package is installed.
#		Enumeration definition:
#			dpkg - dpkg、apt
#			snap
#			rpm
#			yum
#	$3 - no default value, this is an empty reference variable.
# Output parameters:
#	Full app name by fuzzy query.
#
findAppPackageFullNameByKeyword() {
	# Use this command to query the installed app packages associated with string variable $1.
	appPackageLists=
	if [[ $2 == $APP_PACKAGE_INSTALLATION_MODE_DPKG_APT ]]; then

		# Use 'dpkg' command to query.
		appPackageLists=`sudo dpkg --get-selections | grep "$1"`

	elif [[ $2 == $APP_PACKAGE_INSTALLATION_MODE_SNAP ]]; then

		# Use 'snap' command to query.
		appPackageLists=`sudo snap list | grep "$1"`
	fi
	
	# Replace spaces with '#'. This is to better parse string.
	replacedAppPackageLists=`echo $appPackageLists | sed -e 's/[ ]/#/g'`

	# Cut from the left until the first '#' character stops.
	appPackageName=`echo ${replacedAppPackageLists%%#*}`

	# Remove '#'.
	currentAppPackageName=`echo $appPackageName | sed -e 's/[#]//g'`

	eval value1=\$$1
	var1=$1

	eval value2=\$$2
	var2=$2

	eval value3=$currentAppPackageName
	var3=$3

	eval $var1=$value1
	eval $var2=$value2
	eval $var3=$value3
}

#
# Update software repository in sysytem. 
#
# Input paramters: 
#	none
# Output parameters:
#	none
#
updateSoftwareRepository() {
    echo '------------------------- Update software repository.'
    sudo apt-get update
    echo -e '------------------------- Done.\n'
}

#
# Output success message to standard output.
#
# Input paramters: 
#	none
# Output parameters:
#	none
#
outputSuccessMsg() {
	echo -e "\033[32m 🔵 ------------------------- $1\n \033[0m"
}

#
# Output warn message to standard output.
#
# Input paramters: 
#	none
# Output parameters:
#	none
#
outputWarnMsg() {
	echo -e "\033[33m 🔶 ------------------------- $1\n \033[0m"
}

#
# Output error message to standard output.
#
# Input paramters: 
#	none
# Output parameters:
#	none
#
outputErrorMsg() {
	echo -e "\033[31m 🔴 ------------------------- $1\n \033[0m"
}
# ######################### end of functions #########################